﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.BLL
{
    partial class BizPhieuThu
    {
        /// <summary>
        /// Lấy danh sách phiếu thu của một sinh viên
        /// </summary>
        /// <param name="maSinhVien"></param>
        /// <returns></returns>
        internal static List<CustomPhieuThu> GetListPhieuThuFor1SinhVien(string maSinhVien, bool hocPhi, bool lePhi, ref long tongTien)
        {
            using (var context = new QLHPEntities())
            {
                var phieuThu = from pt in context.PhieuThuDbSet
                               where
                               (pt.BienCheSinhVienVaoHocKy.MaSinhVien == maSinhVien) &&
                               ((hocPhi && lePhi) ? true : (hocPhi ? pt.LaHocPhi == true : (lePhi ? pt.LaHocPhi == false : false)))
                               select new CustomPhieuThu()
                               {
                                   sNamHoc = pt.BienCheSinhVienVaoHocKy.HocKy.NamHoc.TenNamHoc,
                                   sHocKy = pt.BienCheSinhVienVaoHocKy.HocKy.TenHocKy,
                                   SoTien = pt.SoTien,
                                   NgayThu = pt.NgayThu,
                                   GhiChu = "",
                                   NoiDungThu = pt.NoiDungThu,
                                   SoPhieu = pt.SoPhieu,
                                   SoQuyen = pt.SoQuyen
                               };
                if (phieuThu != null && phieuThu.Count() > 0)
                    tongTien = phieuThu.Sum(e => e.SoTien);
                else
                    tongTien = 0;
                return phieuThu.ToList();
            }
        }

        /// <summary>
        /// Lấy danh sách các phiếu thu theo điều kiện
        /// </summary>
        /// <param name="dFrom"></param>
        /// <param name="dTo"></param>
        /// <param name="nguoiThu"></param>
        /// <param name="loaiHinh"></param>
        /// <param name="namTS"></param>
        /// <param name="lop"></param>
        /// <returns>[0]: Số bản ghi;[1]: Số tiền</returns>
        internal static List<ThongKePhieuThu> GetThongKe(bool includeTime, DateTime dFrom, DateTime dTo, NguoiSuDung nguoiThu, LoaiHinhDaoTao loaiHinh, string namTS, LopGoc lop, int get, int skip)
        {
            try
            {
                using (var context = new QLHPEntities())
                {
                    var list = (from e in context.PhieuThuDbSet
                                where
                                   (includeTime ? (e.NgayThu >= dFrom && e.NgayThu <= dTo) : true) &&
                                    (nguoiThu.TenDangNhap == "-1000" ? true : e.NguoiSuDung.TenDangNhap == nguoiThu.TenDangNhap) &&
                                    (loaiHinh.MaLoaiHinh == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.MaLoaiHinh == loaiHinh.MaLoaiHinh) &&
                                    (namTS == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.NamTuyenSinh == namTS) &&
                                    (lop.MaLopGoc == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.MaLopGoc == lop.MaLopGoc)
                                orderby e.NgayThu, e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh, e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc descending
                                select new ThongKePhieuThu()
                                {
                                    MaPhieuThu = e.MaPhieuThu,
                                    MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                    HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                    Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                    NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                    GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                    LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                    LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                    NgayNop = e.NgayThu,
                                    SoTien = e.SoTien,
                                    NguoiThu = e.NguoiSuDung.HoTen,
                                    SoPhieu = e.SoPhieu,
                                    SoQuyen = e.SoQuyen
                                }).Skip((skip - 1) * get).Take(get);
                    return list.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Lấy tổng số bản ghi và tổng tiền
        /// </summary>
        /// <param name="dFrom"></param>
        /// <param name="dTo"></param>
        /// <param name="nguoiThu"></param>
        /// <param name="loaiHinh"></param>
        /// <param name="namTS"></param>
        /// <param name="lop"></param>
        /// <param name="sBanGhi"></param>
        /// <param name="soTien"></param>
        internal static void GetThongKe(bool includeTime, DateTime dFrom, DateTime dTo, NguoiSuDung nguoiThu, LoaiHinhDaoTao loaiHinh, string namTS, LopGoc lop, out int? sBanGhi, out double? soTien)
        {
            try
            {
                using (var context = new QLHPEntities())
                {
                    var list = (from e in context.PhieuThuDbSet
                                where
                                    (includeTime ? (e.NgayThu >= dFrom && e.NgayThu <= dTo) : true) &&
                                    (nguoiThu.TenDangNhap == "-1000" ? true : e.NguoiSuDung.TenDangNhap == nguoiThu.TenDangNhap) &&
                                    (loaiHinh.MaLoaiHinh == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.MaLoaiHinh == loaiHinh.MaLoaiHinh) &&
                                    (namTS == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.NamTuyenSinh == namTS) &&
                                    (lop.MaLopGoc == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.MaLopGoc == lop.MaLopGoc)
                                orderby e.NgayThu, e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh, e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc descending
                                select e);
                    sBanGhi = list.Count();
                    if (sBanGhi == 0)
                        soTien = 0;
                    else
                        soTien = list.Sum(t => t.SoTien);
                }
            }
            catch
            {
                sBanGhi = 0;
                soTien = 0;
            }
        }

        /// <summary>
        /// Lấy danh sách phiếu thu theo số quyển và số phiếu
        /// </summary>
        /// <param name="soQuyen"></param>
        /// <param name="soPhieu"></param>
        /// <returns></returns>
        internal static List<ThongKePhieuThu> GetListPhieuThu(string soQuyen, string soPhieu)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                                .Include("BienCheSinhVienVaoHocKy")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien.LopGoc")
                            where
                            ((string.IsNullOrEmpty(soQuyen)) && string.IsNullOrEmpty(soPhieu) ?
                            (e.SoQuyen == soQuyen) && (e.SoPhieu == soPhieu) :
                            ((string.IsNullOrEmpty(soQuyen)) ? (e.SoPhieu == soPhieu) :
                            (string.IsNullOrEmpty(soPhieu) ? (e.SoQuyen == soQuyen) :
                            ((e.SoQuyen == soQuyen) && (e.SoPhieu == soPhieu)))))
                            orderby e.NgayThu, e.SoPhieu ascending
                            select new ThongKePhieuThu()
                            {
                                MaPhieuThu = e.MaPhieuThu,
                                MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                NgayNop = e.NgayThu,
                                SoTien = e.SoTien,
                                NguoiThu = e.NguoiSuDung.HoTen,
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                NoiDungNop = e.NoiDungThu,
                                DaChon = false
                            });
                return list.ToList();
            }
        }


        internal static List<ThongKePhieuThu> GetListPhieuThuTheoLop(string maHocKy, string maLop)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                                .Include("BienCheSinhVienVaoHocKy")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien.LopGoc")
                            where
                           ((e.BienCheSinhVienVaoHocKy.HocKy.MaHocKy == maHocKy) && (e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.MaLopGoc == maLop))
                            orderby e.NgayThu, e.SoPhieu ascending
                            select new ThongKePhieuThu()
                            {
                                MaPhieuThu = e.MaPhieuThu,
                                MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                NgayNop = e.NgayThu,
                                SoTien = e.SoTien,
                                NguoiThu = e.NguoiSuDung.HoTen,
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                NoiDungNop = e.NoiDungThu,
                                DaChon = false
                            });
                return list.ToList();
            }
        }


        /// <summary>
        /// Thêm mới một phiếu thu
        /// </summary>
        /// <param name="phieuThu"></param>
        /// <returns></returns>
        internal static bool CreatePhieuThu(PhieuThu phieuThu)
        {
            using (var context = new QLHPEntities())
            {
                try
                {
                    context.PhieuThuDbSet.Add(phieuThu);
                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Lấy lịch sử nộp học phí/lệ phí trên mỗi học kỳ của 1 sinh viên
        /// </summary>
        /// <param name="maSinhVien"></param>
        /// <param name="maHocKy"></param>
        /// <param name="laHocPhi">True: Học phí; False: Lệ phí</param>
        /// <returns></returns>
        internal static object GetLichSuNopLePhi1HocKy(string maSinhVien, string maHocKy, ref long tongTienDaNop, bool laHocPhi = true)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                            where
                            (e.BienCheSinhVienVaoHocKy.MaHocKy == maHocKy) &&
                            (e.BienCheSinhVienVaoHocKy.MaSinhVien == maSinhVien) &&
                            (laHocPhi ? e.LaHocPhi == true : e.LaHocPhi == false)
                            orderby e.NgayThu descending
                            select new CustomPhieuThu()
                            {
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                MaPhieuThu = e.MaPhieuThu,
                                NgayThu = e.NgayThu,
                                SoTien = e.SoTien,
                                NoiDungThu = e.NoiDungThu
                            });
                if (list.Count() > 0)
                    tongTienDaNop = list.Sum(e => e.SoTien);
                else
                    tongTienDaNop = 0;
                return list.ToList();
            }
        }

        /// <summary>
        /// Cập nhật thông tin phiếu thu
        /// </summary>
        /// <param name="phieuThu"></param>
        /// <returns></returns>
        internal static bool Update(PhieuThu phieuThu)
        {
            using (var context = new QLHPEntities())
            {
                try
                {
                    var pt = (from e in context.PhieuThuDbSet
                              where e.MaPhieuThu == phieuThu.MaPhieuThu
                              select e).SingleOrDefault();
                    if (pt != null)
                    {
                        pt.SoQuyen = phieuThu.SoQuyen;
                        pt.SoPhieu = phieuThu.SoPhieu.IsNullOrEmpty() ? "" : int.Parse(phieuThu.SoPhieu).ToString();
                        pt.NoiDungThu = phieuThu.NoiDungThu;
                        pt.SoTien = phieuThu.SoTien;
                        pt.NgayThu = phieuThu.NgayThu;
                        pt.DaIn = phieuThu.DaIn;
                        context.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// Chỉ cập nhật ngày thu của phiếu
        /// </summary>
        /// <param name="phieuThu"></param>
        /// <returns></returns>
        internal static bool UpdateNgayThu(PhieuThu phieuThu)
        {
            using (var context = new QLHPEntities())
            {
                try
                {
                    var pt = (from e in context.PhieuThuDbSet
                              where e.MaPhieuThu == phieuThu.MaPhieuThu
                              select e).SingleOrDefault();
                    if (pt != null)
                    {
                        pt.NgayThu = phieuThu.NgayThu;
                        context.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static object GetListPhieuThu(LopGoc lopGoc, HocKy hocKy, bool chkHocPhi, bool chkLePhi)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                            where
                            (e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.MaLopGoc == lopGoc.MaLopGoc) &&
                            (e.BienCheSinhVienVaoHocKy.MaHocKy == hocKy.MaHocKy) &&
                            (chkHocPhi && chkLePhi ? true : (chkHocPhi ? e.LaHocPhi == true : (chkLePhi ? e.LaHocPhi == false : false)))
                            orderby e.NgayThu, e.SoPhieu descending
                            select new ThongKePhieuThu()
                            {
                                MaPhieuThu = e.MaPhieuThu,
                                MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                NgayNop = e.NgayThu,
                                SoTien = e.SoTien,
                                NguoiThu = e.NguoiSuDung.HoTen,
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                NoiDungNop = e.NoiDungThu,
                                DaChon = false,
                                LaHocPhi = e.LaHocPhi
                            });
                return list.ToList();
            }
        }

        /// <summary>
        /// Lấy về phiếu thu đầy đủ các thông tin
        /// </summary>
        /// <param name="maPhieuThu"></param>
        /// <returns></returns>
        internal static PhieuThu GetPhieuThuFull(string maPhieuThu)
        {
            using (var context = new QLHPEntities())
            {
                var pt = (from e in context.PhieuThuDbSet.Include("BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao").Include("NguoiSuDung")
                          where e.MaPhieuThu == maPhieuThu
                          select e).SingleOrDefault();
                if (pt != null)
                {
                    pt.SoPhieu = pt.SoPhieu.GenerateSoPhieu();
                    return pt;
                }
                else
                    return null;
            }
        }

        internal static List<ThongKeLop> GetThongKeTheoLop(string maLopGoc, bool LaHocPhi = true)
        {
            using (var context = new QLHPEntities())
            {
                List<ThongKeLop> dsKQ = new List<ThongKeLop>();
                var lsSV = from e in context.SinhVienDbSet
                           where e.MaLopGoc == maLopGoc
                           orderby e.Ten, e.HoDem ascending
                           select e;
                foreach (var sv in lsSV)
                {
                    var thongKeLop = new ThongKeLop();
                    thongKeLop.MaSinhVien = sv.MaSinhVien;
                    thongKeLop.HoDem = sv.HoDem;
                    thongKeLop.Ten = sv.Ten;
                    var lsBienChe = from e in context.BienCheSinhVienVaoHocKiDbSet
                                    where e.MaSinhVien == sv.MaSinhVien
                                    select e;
                    foreach (var bc in lsBienChe)
                    {
                        var soTTHocKu = ThuTuHocKy(sv.LopGoc.NamTuyenSinh, bc.HocKy.NamHoc.TenNamHoc, bc.HocKy.TenHocKy);
                        var lsPT = (from e in context.PhieuThuDbSet
                                    where
                                    (e.MaBienChe == bc.MaBienChe) &&
                                    (LaHocPhi ? e.LaHocPhi == true : e.LaHocPhi == false)
                                    select e).ToList();
                        if (lsPT == null || lsPT.Count == 0)
                            continue;
                        int? tien = 0;
                        tien = lsPT.Sum(e => e.SoTien);
                        switch (soTTHocKu)
                        {
                            case 1:
                                thongKeLop.HK1 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 2: thongKeLop.HK2 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 3: thongKeLop.HK3 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 4: thongKeLop.HK4 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 5: thongKeLop.HK5 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 6: thongKeLop.HK6 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 7: thongKeLop.HK7 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 8: thongKeLop.HK8 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 9: thongKeLop.HK9 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                            case 10: thongKeLop.HK10 = tien != null ? tien.Value.ToString().FormatCurrency() : "";
                                break;
                        }
                    }
                    dsKQ.Add(thongKeLop);
                }
                return dsKQ;
            }
        }

        internal static int ThuTuHocKy(string namTS, string NamHoc, int HK)
        {
            try
            {
                int namTSInt = int.Parse(namTS);
                string namDau = NamHoc.Split(new char[] { '-' })[0];
                int namDauInt = int.Parse(namDau);
                if (HK == 1)
                {
                    if (namTSInt == namDauInt)
                        return 1;
                    if (namTSInt == (namDauInt - 1))
                        return 3;
                    if (namTSInt == (namDauInt - 2))
                        return 5;
                    if (namTSInt == (namDauInt - 3))
                        return 7;
                    if (namTSInt == (namDauInt - 4))
                        return 9;
                }
                else if (HK == 2)
                {
                    if (namTSInt == namDauInt)
                        return 2;
                    if (namTSInt == (namDauInt - 1))
                        return 4;
                    if (namTSInt == (namDauInt - 2))
                        return 6;
                    if (namTSInt == (namDauInt - 3))
                        return 8;
                    if (namTSInt == (namDauInt - 4))
                        return 10;
                }
                return 0;
            }
            catch
            {
                return 0;
            }
        }

        internal static string TaoNamHoc(int nam)
        {
            return nam + "-" + (nam + 1).ToString();
        }

        internal static bool Delete(string maPhieuThu)
        {
            using (var context = new QLHPEntities())
            {
                try
                {
                    var pt = (from e in context.PhieuThuDbSet
                              where e.MaPhieuThu == maPhieuThu
                              select e).SingleOrDefault();
                    if (pt != null)
                    {
                        context.PhieuThuDbSet.Remove(pt);
                        context.SaveChanges();
                        return true;
                    }
                    else
                        return false;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static List<ThongKePhieuThu> GetListPhieuThu(int soTien)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                                .Include("BienCheSinhVienVaoHocKy")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien.LopGoc")
                            where
                           (e.SoTien == soTien)
                            orderby e.NgayThu, e.SoPhieu ascending
                            select new ThongKePhieuThu()
                            {
                                MaPhieuThu = e.MaPhieuThu,
                                MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                NgayNop = e.NgayThu,
                                SoTien = e.SoTien,
                                NguoiThu = e.NguoiSuDung.HoTen,
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                NoiDungNop = e.NoiDungThu,
                                DaChon = false
                            });
                return list.ToList();
            }
        }

        internal static List<ThongKePhieuThu> GetListPhieuThuTheoHoTen(string hoTen, string maLop)
        {
            using (var context = new QLHPEntities())
            {
                var list = (from e in context.PhieuThuDbSet
                                .Include("BienCheSinhVienVaoHocKy")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien")
                                .Include("BienCheSinhVienVaoHocKy.SinhVien.LopGoc")
                            where
                            (
                           (e.BienCheSinhVienVaoHocKy.SinhVien.HoDem + e.BienCheSinhVienVaoHocKy.SinhVien.Ten).Contains(hoTen) &&
                           (maLop == "-1000" ? true : e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.MaLopGoc == maLop)
                           )
                            orderby e.BienCheSinhVienVaoHocKy.SinhVien.Ten ascending
                            select new ThongKePhieuThu()
                            {
                                MaPhieuThu = e.MaPhieuThu,
                                MaSinhVien = e.BienCheSinhVienVaoHocKy.SinhVien.MaSinhVien,
                                HoDem = e.BienCheSinhVienVaoHocKy.SinhVien.HoDem,
                                Ten = e.BienCheSinhVienVaoHocKy.SinhVien.Ten,
                                NgaySinh = e.BienCheSinhVienVaoHocKy.SinhVien.NgaySinh,
                                GioiTinh = e.BienCheSinhVienVaoHocKy.SinhVien.GioiTinh,
                                LopGoc = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.TenLopGoc,
                                LoaiHinhDaoTao = e.BienCheSinhVienVaoHocKy.SinhVien.LopGoc.LoaiHinhDaoTao.TenLoaiHinh,
                                NgayNop = e.NgayThu,
                                SoTien = e.SoTien,
                                NguoiThu = e.NguoiSuDung.HoTen,
                                SoPhieu = e.SoPhieu,
                                SoQuyen = e.SoQuyen,
                                NoiDungNop = e.NoiDungThu,
                                DaChon = false
                            });
                return list.ToList();
            }
        }
    }

    [System.ComponentModel.DataAnnotations.Schema.NotMapped]
    class CustomPhieuThu : PhieuThu
    {
        public string SoPhieuNew { get { return SoPhieu.GenerateSoPhieu(); } }
        public string sNamHoc { get; set; }
        public int sHocKy { get; set; }
        public string HocKyFull
        {
            get
            {
                return sHocKy + "," + sNamHoc;
            }
        }
        public string SoBienLai { get { return SoQuyen + " - " + SoPhieu.GenerateSoPhieu(); } }
        public string GhiChu { get; set; }
        public string NgayThuStr
        {
            get
            {
                if (NgayThu != null) return string.Format("{0:dd/MM/yyyy}", NgayThu);
                else return "";
            }
        }
        public string SoTienStr { get { return SoTien.ToString().FormatCurrency(); } }
    }
    [System.ComponentModel.DataAnnotations.Schema.NotMapped]
    class ThongKePhieuThu
    {
        public string MaPhieuThu { get; set; }
        public string MaSinhVien { get; set; }
        public string HoDem { get; set; }
        public string Ten { get; set; }
        public string NgaySinh { get; set; }
        public bool GioiTinh { get; set; }
        public string GioiTinhStr { get { return GioiTinh == true ? "Nam" : "Nữ"; } }
        public string LopGoc { get; set; }
        public string LoaiHinhDaoTao { get; set; }
        public DateTime NgayNop { get; set; }
        public string NgayNopStr { get { return string.Format("{0:dd/MM/yyyy}", NgayNop); } }
        public string SoBienLai { get { return SoQuyen + " - " + SoPhieu.GenerateSoPhieu(); } }
        public int SoTien { get; set; }
        public string SoTienStr { get { return SoTien.ToString().FormatCurrency(); } }
        public string NguoiThu { get; set; }
        public string SoQuyen { get; set; }
        public string SoPhieu { get; set; }
        public string SoPhieuNew { get { return SoPhieu.GenerateSoPhieu(); } }
        public string NoiDungNop { get; set; }
        public string ThaoTacXoa { get { return "Xóa"; } }
        public string ThaoTacChinhSua { get { return "Chỉnh sửa"; } }
        public bool DaChon { get; set; }
        public bool LaHocPhi { get; set; }
    }

    class ThongKeLop
    {
        public string MaSinhVien { get; set; }
        public string HoDem { get; set; }
        public string Ten { get; set; }
        public string HK1 { get; set; }
        public string HK2 { get; set; }
        public string HK3 { get; set; }
        public string HK4 { get; set; }
        public string HK5 { get; set; }
        public string HK6 { get; set; }
        public string HK7 { get; set; }
        public string HK8 { get; set; }
        public string HK9 { get; set; }
        public string HK10 { get; set; }
    }
}
